﻿using System.Runtime.CompilerServices;

namespace zijian666.Data;

internal class DiagnosticData(string name, string content, Exception? ex, string file, string member, int line)
    : DynamicRecord(INDEXER, [name, content, ex, file, member, line])
{
    private static readonly Dictionary<string, int> INDEXER = new(StringComparer.OrdinalIgnoreCase)
    {
        [nameof(Name)] = 0,
        [nameof(Content)] = 1,
        [nameof(Exception)] = 2,
        [nameof(File)] = 3,
        [nameof(Member)] = 4,
        [nameof(Line)] = 5,
    };

    private dynamic GetPropertyValue([CallerMemberName] string member = null!) => this[member];

    public string Name => GetPropertyValue();
    public string Content => GetPropertyValue();
    public Exception? Exception => GetPropertyValue();
    public string File => GetPropertyValue();
    public string Member => GetPropertyValue();
    public string Line => GetPropertyValue();

    public override string ToString()
    {
        if (Exception == null)
        {
            return $"{Name}: {Content}";
        }
        return $"{Name}: {Content}\n{File}#{Member}:{Line}\n{Exception}";
    }
}
